{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "默认情况下，StateGraph使用单一模式运行，所有节点都应该使用该模式进行通信。然而，也可以为一个图定义不同的输入和输出模式。\n",
    "当指定不同的模式时，内部模式仍将用于节点之间的通信。输入模式确保提供的输入与预期的结构匹配，而输出模式根据定义的输出模式过滤内部数据，只返回相关信息。\n",
    "在这个例子中，我们将看到如何定义不同的输入和输出模式。\n",
    "\n",
    "> 仅使用与invoke()\n",
    "\n",
    "## Define and use the graph\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "9c55aec67ac9fb6"
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'answer': 'bye'}\n"
     ]
    }
   ],
   "source": [
    "from langgraph.graph import StateGraph, START, END\n",
    "from typing_extensions import TypedDict\n",
    "\n",
    "# Define the schema for the input\n",
    "class InputState(TypedDict):\n",
    "    question: str\n",
    "\n",
    "\n",
    "# Define the schema for the output\n",
    "class OutputState(TypedDict):\n",
    "    answer: str\n",
    "\n",
    "\n",
    "# Define the overall schema, combining both input and output\n",
    "class OverallState(InputState, OutputState):\n",
    "    pass\n",
    "\n",
    "\n",
    "# Define the node that processes the input and generates an answer\n",
    "def answer_node(state: InputState):\n",
    "    # Example answer and an extra key\n",
    "    return {\"answer\": \"bye\", \"question\": state[\"question\"]}\n",
    "\n",
    "\n",
    "# Build the graph with input and output schemas specified\n",
    "builder = StateGraph(OverallState, input=InputState, output=OutputState)\n",
    "builder.add_node(answer_node)  # Add the answer node\n",
    "builder.add_edge(START, \"answer_node\")  # Define the starting edge\n",
    "builder.add_edge(\"answer_node\", END)  # Define the ending edge\n",
    "graph = builder.compile()  # Compile the graph\n",
    "# Invoke the graph with an input and print the result\n",
    "print(graph.invoke({\"question\": \"hi\"}))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-11-11T03:33:06.819396Z",
     "start_time": "2024-11-11T03:33:04.898647Z"
    }
   },
   "id": "b5e71435d5ec9e18",
   "execution_count": 1
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "7e903ac4d7cb6c69"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
